package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiType;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiT_cityService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE)
public class T_typeController {

	private static Logger logger = Logger.getLogger(T_typeController.class);

	@Autowired
	private ApiShopsService apiShopsService;
	@Autowired
	private ApiT_cityService apiT_cityService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private StartOnLoadService startOnLoadService;

	/**
	 * 商品中心列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "type-list")
	public ModelAndView type_list(HttpServletRequest request,
			HttpServletResponse response, PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/type_list.jsp");

		List<ApiShop> shops = apiShopsService.findShops();
		mv.addObject("shops", shops);
		List<ApiType> typespage = apiTypeService.getzyListPage(pageInfo);
		List<ApiType> types = apiTypeService.getTypesZiYing();
		mv.addObject("types", typespage);
		mv.addObject("alltypes", types);
		mv.addObject("pageInfo", pageInfo);

		List<ApiType> typess = apiTypeService.getBigTypesZiYing();
		mv.addObject("typesOne", typess);
		List<ApiType> types2 = apiTypeService.getSecondTypes();
		mv.addObject("typesTwo", types2);

		return mv;
	}

	/**
	 * 营销活动分类列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "act_type-list")
	public ModelAndView act_type_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/act_type_list.jsp");
		//
		List<ApiShop> shops = apiShopsService.findShops();
		mv.addObject("shops", shops);

		List<ApiType> types = apiTypeService.findTypesByCmod("营销活动");
		mv.addObject("types", types);

		return mv;
	}

	/**
	 * 商家中心列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "shops_type-list")
	public ModelAndView shops_type_list(HttpServletRequest request,
			HttpServletResponse response, PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/type_list2.jsp");
		//
		List<ApiShop> shops = apiShopsService.findShops();
		mv.addObject("shops", shops);
		List<ApiType> typespage = apiTypeService.getfeizyListPage(pageInfo);
		List<ApiType> types = apiTypeService.getTypes();
		mv.addObject("types", typespage);
		mv.addObject("alltypes", types);

		return mv;
	}

	/**
	 * 进入新增页面（商家中心）
	 * 
	 * @return
	 */
	@RequestMapping(value = "type-add-pre")
	public ModelAndView type_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (request.getParameter("id") != null) {
			ModelAndView mv = new ModelAndView(
					"forward:/backstage/type_update.jsp");
			List<ApiShop> shops = apiShopsService.searchShopsByEnable(1);
			mv.addObject("shops", shops);

			List<ApiType> types = apiTypeService.getBigTypes();
			mv.addObject("typesOne", types);

			List<ApiType> types2 = apiTypeService.getSecondTypes();
			mv.addObject("typesTwo", types2);
			String id = request.getParameter("id");
			ApiType type = apiTypeService.findTypeById(Integer.valueOf(id));
			mv.addObject("type", type);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView(
					"forward:/backstage/type_add.jsp");
			List<ApiShop> shops = apiShopsService.searchShopsByEnable(1);
			mv.addObject("shops", shops);

			List<ApiType> types = apiTypeService.getBigTypes();
			mv.addObject("typesOne", types);

			List<ApiType> types2 = apiTypeService.getSecondTypes();
			mv.addObject("typesTwo", types2);
			return mv;
		}
	}

	/**
	 * 进入新增页面2（商品中心）
	 * 
	 * @return
	 */
	@RequestMapping(value = "type-add-pre2")
	public ModelAndView type_add_pre2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (request.getParameter("id") != null) {
			ModelAndView mv = new ModelAndView(
					"forward:/backstage/type_update2.jsp");
			List<ApiShop> shops = apiShopsService.searchShopsByEnable(1);
			mv.addObject("shops", shops);
			List<ApiType> types = apiTypeService.getBigTypesZiYing();
			mv.addObject("typesOne", types);
			List<ApiType> types2 = apiTypeService.getSecondTypes();
			mv.addObject("typesTwo", types2);
			String id = request.getParameter("id");
			ApiType type = apiTypeService.findTypeById(Integer.valueOf(id));
			mv.addObject("type", type);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView(
					"forward:/backstage/type_add2.jsp");
			List<ApiShop> shops = apiShopsService.searchShopsByEnable(1);
			mv.addObject("shops", shops);
			List<ApiType> types = apiTypeService.getBigTypesZiYing();
			mv.addObject("typesOne", types);
			List<ApiType> types2 = apiTypeService.getSecondTypes();
			mv.addObject("typesTwo", types2);
			return mv;
		}
	}

	/**
	 * 进入新增活动品类
	 * 
	 * @return
	 */
	@RequestMapping(value = "type-add-act")
	public ModelAndView type_add_act(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/act_type_add.jsp");
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiType type = apiTypeService.findTypeById(Integer.valueOf(id));
			mv.addObject("type", type);
		}
		return mv;
	}

	@RequestMapping(value = "findsecond")
	public void findsecond(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String cCode = request.getParameter("cCode");
		List<ApiType> typeseconds = apiTypeService.bybelong(Integer
				.parseInt(cCode));
		map.put("typeseconds", typeseconds);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 根据cName查找自营
	@RequestMapping(value = "findbycnamezy")
	public ModelAndView findbycnamezy(HttpServletRequest request,
			HttpServletResponse response, PageInfo pageInfo) throws Exception {
		String cName = request.getParameter("cName2");
		String cGrade = request.getParameter("cGrade");
		String cBelong = request.getParameter("cBelong");
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("cGrade", cGrade);
		conditions.put("cBelong", cBelong);
		conditions.put("cName", cName);
		request.setAttribute("cName2", cName);
		ModelAndView mv = new ModelAndView("forward:/backstage/type_list.jsp");
		//
		List<ApiShop> shops = apiShopsService.findShops();
		mv.addObject("shops", shops);
		System.out.println("自营品类查询入参：" + cGrade + "-" + cBelong);
		List<ApiType> types = apiTypeService.bycnamezyListPage(conditions,
				pageInfo);
		List<ApiType> alltypes = apiTypeService.getTypesZiYing();
		mv.addObject("types", types);
		mv.addObject("alltypes", alltypes);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);

		List<ApiType> typess = apiTypeService.getBigTypesZiYing();
		mv.addObject("typesOne", typess);
		List<ApiType> types2 = apiTypeService.getSecondTypes();
		mv.addObject("typesTwo", types2);

		return mv;
	}

	/**
	 * 只看大类
	 * 
	 * @param request
	 * @param response
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "onlyBigType")
	public ModelAndView onlyBigType(HttpServletRequest request,
			HttpServletResponse response, PageInfo pageInfo) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("onlyBig", "yes");
		List<ApiType> types = apiTypeService.bycnamezyListPage(conditions,
				pageInfo);
		ModelAndView mv = new ModelAndView("forward:/backstage/type_list.jsp");
		mv.addObject("types", types);
		return mv;
	}

	// 根据cName查找非自营
	@RequestMapping(value = "findbycnamefzy")
	public ModelAndView findbycnamefzy(HttpServletRequest request,
			HttpServletResponse response, PageInfo pageInfo) throws Exception {
		String cName = request.getParameter("cName2");
		String shopName = request.getParameter("shopName");
		String city_id = request.getParameter("city_id");
		String type1 = request.getParameter("type1");
		String type2 = request.getParameter("type2");
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("cName", cName);
		conditions.put("shopName", shopName);// 店铺名
		conditions.put("city_id", city_id);// 店铺所属城市
		conditions.put("type1", type1);
		conditions.put("type2", type2);

		ModelAndView mv = new ModelAndView("forward:/backstage/type_list2.jsp");
		//
		List<ApiShop> shops = apiShopsService.findShops();
		mv.addObject("shops", shops);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);

		List<ApiType> types = apiTypeService.bycnamefzyListPage(conditions,
				pageInfo);
		mv.addObject("types", types);
		List<ApiType> types1 = null;
		types1 = apiTypeService.getBigTypes();

		// 包含根级分类
		// types.addAll(types1);
		List<ApiType> types2 = apiTypeService.getSecondTyes(false);
		// types.addAll(types2);
		List<ApiType> alltypes = apiTypeService.getTypes();
		mv.addObject("alltypes", alltypes);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		mv.addObject("types1", types1);
		mv.addObject("types2", types2);
		return mv;
	}

	/**
	 * 新增商品
	 * 
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "type-add")
	public void type_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String cName = request.getParameter("cName");
		String cGrade = request.getParameter("cGrade");
		String cBelong = request.getParameter("cBelong");
		String cKeyword = request.getParameter("cKeyword");
		String cSort = request.getParameter("cSort");
		String cDes = request.getParameter("cDes");
		String cMod = request.getParameter("cMod");
		String cIco = request.getParameter("cIco");
		String cAlias = request.getParameter("cAlias");
		String cIsziying = request.getParameter("cIsziying");
		String sources = request.getParameter("sources");
		String cSid = request.getParameter("cSid");
		if (request.getParameter("cSid") == null) {
			cSid = "0";
		}
		if (Integer.parseInt(cBelong) == 0 && Integer.parseInt(cGrade) != 0) {
			cBelong = cGrade;
		}
		//
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiType t_type = new ApiType();
			if (cSid.equals("0") || cSid == null) {
				t_type.setcSid(0);
			}
			if (cSid != null && !cSid.equals("0")) {
				t_type.setcSid(Integer.parseInt(cSid));
			}
			t_type.setcName(cName);
			t_type.setcGrade(Integer.valueOf(cGrade));
			t_type.setcBelong(Integer.valueOf(cBelong));
			t_type.setcKeyword(cKeyword);
			t_type.setcSort(Integer.valueOf(cSort));
			t_type.setcDes(cDes);
			t_type.setcEnable(1);
			t_type.setcStatus(1);
			t_type.setcIsziying(Integer.valueOf(cIsziying));
			t_type.setcMod(cMod);
			t_type.setcIco(cIco);
			t_type.setcAlias(cAlias);
			t_type.setSources(sources);
			apiTypeService.insertType(t_type);

			map.put(Utils.MESSAGE, "添加成功！");
		}
		// edit
		else {
			ApiType t_type = apiTypeService.findTypeById(Integer.valueOf(id));
			t_type.setcName(cName);
			t_type.setcKeyword(cKeyword);
			t_type.setcGrade(Integer.valueOf(cGrade));
			t_type.setcBelong(Integer.valueOf(cBelong));
			if (cSid.equals("0") || cSid == null) {
				t_type.setcSid(0);
			}
			if (cSid != null && !cSid.equals("0")) {
				t_type.setcSid(Integer.parseInt(cSid));
			}
			t_type.setcSort(Integer.valueOf(cSort));
			t_type.setcDes(cDes);
			t_type.setcIco(cIco);
			t_type.setcAlias(cAlias);
			t_type.setcMod(cMod);
			t_type.setcIsziying(Integer.valueOf(cIsziying));
			t_type.setSources(sources);
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String d = format.format(new Date());
			t_type.setUpdatetime(d);
			apiTypeService.updateType(t_type);
			map.put(Utils.MESSAGE, "编辑成功！");
		}

		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 新增活动品类
	 * 
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "act-type-add")
	public void act_type_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String cName = request.getParameter("cName");
		String cGrade = request.getParameter("cGrade");
		String cBelong = request.getParameter("cBelong");
		String cKeyword = request.getParameter("cKeyword");
		String cSort = request.getParameter("cSort");
		String cDes = request.getParameter("cDes");
		String cMod = request.getParameter("cMod");
		String cIco = request.getParameter("cIco");
		String cAlias = request.getParameter("cAlias");
		String cIsziying = request.getParameter("cIsziying");
		String sources = request.getParameter("sources");
		if (cBelong != null && !"".equals(cBelong) && cGrade != null
				&& !"".equals(cGrade)) {
			if (Integer.parseInt(cBelong) == 0 && Integer.parseInt(cGrade) != 0) {
				cBelong = cGrade;
			}
		}
		//
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiType t_type = new ApiType();
			t_type.setcName(cName);
			t_type.setcGrade(Integer.valueOf(cGrade));
			t_type.setcBelong(Integer.valueOf(cBelong));
			t_type.setcKeyword(cKeyword);
			t_type.setcSort(Integer.valueOf(cSort));
			t_type.setcDes(cDes);
			t_type.setcSid(0);
			t_type.setcEnable(1);
			t_type.setcStatus(1);
			t_type.setcIsziying(1);
			t_type.setcMod(cMod);
			t_type.setcIco(cIco);
			t_type.setcAlias(cAlias);
			t_type.setSources(sources);
			apiTypeService.insertType(t_type);

			map.put(Utils.MESSAGE, "添加成功！");
		}
		// edit
		else {
			ApiType t_type = apiTypeService.findTypeById(Integer.valueOf(id));
			t_type.setcName(cName);
			t_type.setcGrade(Integer.valueOf(cGrade));
			t_type.setcBelong(Integer.valueOf(cBelong));
			t_type.setcKeyword(cKeyword);
			t_type.setcSort(Integer.valueOf(cSort));
			t_type.setcDes(cDes);
			t_type.setcSid(0);
			t_type.setcEnable(1);
			t_type.setcStatus(1);
			t_type.setcIsziying(1);
			t_type.setcMod(cMod);
			t_type.setcIco(cIco);
			t_type.setcAlias(cAlias);
			t_type.setSources(sources);
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String d = format.format(new Date());
			t_type.setUpdatetime(d);
			apiTypeService.updateType(t_type);
			map.put(Utils.MESSAGE, "编辑成功！");
		}

		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "type-disable")
	public void type_disable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// virtual-delete
		ApiType type = apiTypeService.findTypeById(Integer.valueOf(id));
		type.setcEnable(0);
		apiTypeService.updateType(type);

		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "软删除成功！");

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 上传缩略图
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "type-logo-upload")
	public void type_logo_upload(
			@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			String pic_path = ImageUtils.uploadFile(request, "/type/", file);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", pic_path);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		//
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
